package be.vdab.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.dao.SausenDAO;
import be.vdab.entities.Saus;

@WebServlet("/saus/ingredient")
public class SausPerIngredientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final static String VIEW = "/WEB-INF/JSP/sausperingredient.jsp";
	private final SausenDAO sausenDAO = new SausenDAO();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (!request.getParameterMap().isEmpty()) {

			String ingredient = new String(request.getParameter("ingredient"));
			if (ingredient != null) {
				Map<String, String> fouten = new HashMap<String, String>();
				if (ingredient.isEmpty()) {
					fouten.put("ingredient", "verplicht");
				} else {
					List<Saus> sausen = sausenDAO.findByIngredient(ingredient);
					if (sausen.isEmpty()) {
						fouten.put("nietgevonden", "geen sausen gevonden");
					}
					request.setAttribute("sausen", sausen);
				}				
				request.setAttribute("fouten", fouten);
			}			
		}		
		request.getRequestDispatcher(VIEW).forward(request, response);		
	}
}
